home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / Threads Interface / thread stresser source / CTestThread.c next >
Encoding:
C/C++ Source or Header  |  1994-04-03  |  1.3 KB  |  98 lines  |  [TEXT/KAHL]

  1. /***
  2.  * CTestThread.c
  3.  *
  4.  *  Method to do the threads
  5.  *        Copyright © Gordon Watts 1994 (gwatts@fnal.fnal.gov)
  6.  *
  7.  ***/
  8. #include "CTestThread.h"
  9. #include "Exceptions.h"
  10. #include "util.h"
  11.  
  12. /**
  13.  * CTestThread
  14.  *
  15.  *  Make sure we delete ourselves when we quit...
  16.  *
  17.  **/
  18. CTestThread :: CTestThread (void)
  19. {
  20.     DeleteOnFinish (true);
  21.     quitNow = false;
  22.     theStackSize = 2048;
  23. }
  24. /**
  25.  * ThreadRoutine
  26.  *
  27.  *  Called to do the work...
  28.  *
  29.  **/
  30. void CTestThread :: ThreadRoutine (void)
  31. {
  32.     extern short numActive;
  33.     short    randNum;
  34.  
  35.     /**
  36.      ** Let everyone know we are now up and running...
  37.      **/
  38.     
  39.     numActive++;
  40.     TRY {
  41.     
  42.         /**
  43.          ** Loop till we quit or do something else nasty
  44.          **/
  45.         
  46.         while (!quitNow) {
  47.             randNum = Random ();
  48.             randNum = randNum < 0 ? -randNum : randNum;
  49.  
  50.             randNum = randNum % 100;
  51.             
  52.             DoTest (randNum);
  53.         }
  54.         
  55.     } CATCH {
  56.         numActive --;
  57.     } ENDTRY;
  58.  
  59.     /**
  60.      ** Let em know we are ok and we are quitting...
  61.      **/
  62.  
  63.     numActive--;
  64. }
  65.  
  66. /**
  67.  * DoTest
  68.  *
  69.  *  Do test number "num".
  70.  *
  71.  **/
  72. void CTestThread :: DoTest (short thisTest)
  73. {
  74.     unsigned long howLong;
  75.  
  76.     switch (thisTest) {
  77.         case 99:
  78.             quitNow = true;
  79.             break;
  80.  
  81.         case 98:
  82.             FailOSErr (-2938);
  83.             break;
  84.         
  85.         case 97:
  86.             SysBeep (10);
  87.             break;
  88.  
  89.         default:
  90.             if (thisTest < 20) {
  91.                 YieldToAnyThread ();
  92.             } else {
  93.                 howLong = (Random () % 120) + TickCount();
  94.                 SleepForAWhile (howLong);
  95.             }
  96.             break;
  97.     }
  98. }